﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;

namespace BOMS.WebUI.Web.DataAnalyse
{
    public partial class singleHistoryPic : System.Web.UI.Page
    {
       public byte Generation
        {
            get { return byte.Parse(Request.QueryString["generation"]); }
        }
       public string EquipID
       {
           get { return Request.QueryString["equipid"]; }
       }
       public string BatGrp
       {
           get { return Request.QueryString["batgrp"]; }
       }
       public int BatteryID
       {
           get { return int.Parse(Request.QueryString["batid"]); }
       }
       public DateTime DtmBegin
       {
           get
           {
               if (!string.IsNullOrEmpty(Request.QueryString["dtmbegin"]))
                   return DateTime.Parse(Request.QueryString["dtmbegin"]);
               else
                   return new DateTime(2010,1,1);
           }
       }
       public DateTime DtmEnd
       {
           get
           {
               if (!string.IsNullOrEmpty(Request.QueryString["dtmend"]))
                   return DateTime.Parse(Request.QueryString["dtmend"]);
               else
                   return new DateTime(2050,1,1);
           }
       }
       public DateTime DtmRealBegin
       {
           get {
               if (ViewState["_DtmRealBegin"] == null)
               {
                   SetRealDtm2ViewState();
               }
               return (DateTime)ViewState["_DtmRealBegin"];
           }          
       }
       public DateTime DtmRealEnd
       {
           get
           {
               if (ViewState["_DtmRealEnd"] == null)
               {
                   SetRealDtm2ViewState();
               }
               return (DateTime)ViewState["_DtmRealEnd"];
           }
       }

       //public int sel;
        protected void Page_Load(object sender, EventArgs e)
        {
            lnbBeForeChart.Click += new EventHandler(lnbChart_Click);
            lnbBehindChart.Click += new EventHandler(lnbChart_Click);
            if (!IsPostBack)
            {
                BLL.M_SubLans bll = new BLL.M_SubLans();
                Model.M_SubLans model = bll.GetModelByEquipMac(EquipID);

                lbIntro.Text = string.Format("按条件生成 现场主机:<b>{0}</b> 电池组:<b>{1}</b> 电池:<b>{2}</b> 从 <b>{3}</b> 到 <b>{4}</b> 时间内的图形报表",
                    model.HostName,
                    BatGrp,
                    BatteryID,
                    DtmRealBegin,
                    DtmRealEnd
                    );
            }
        }

        void lnbChart_Click(object sender, EventArgs e)
        {
            LinkButton lnb = (LinkButton)sender;
            if (lnb.CommandName == "Preview")
            {
                hdfCurrentPageIndex.Value = (int.Parse(hdfCurrentPageIndex.Value) - 1).ToString();
            }
            else if (lnb.CommandName == "Nextview")
            {
                hdfCurrentPageIndex.Value = (int.Parse(hdfCurrentPageIndex.Value) + 1).ToString();
            }
            SingleBtn_Click(null, null);
        }
        private void SetRealDtm2ViewState()
        {
           BLL.S_Warning bll = new BLL.S_Warning();
           DateTime?[] dtms = bll.GetRealLogTime(EquipID, BatGrp, BatteryID);
           if (dtms[0] != null && dtms[0].HasValue)
           {
               ViewState["_DtmRealBegin"] = dtms[0].Value;
           }
           else
           {
               ViewState["_DtmRealBegin"] = new DateTime(2010, 1, 1);
           }
           if (dtms[1] != null && dtms[1].HasValue)
           {
               ViewState["_DtmRealEnd"] = dtms[1].Value;
           }
           else
           {
               ViewState["_DtmRealEnd"] = new DateTime(2050, 1, 1);
           }
        }
        //void BtnZxt_Click(object sender, ImageClickEventArgs e)
        //{
        //    ChartOutput1.Visible = true;
        //    tblBtnImgs.Visible = false;

        //    ImageButton imgbtn = sender as ImageButton;
        //    if (imgbtn.ID == "BtnZxt")
        //    {
        //        ChartOutput1.ChartType = SeriesChartType.Line;
        //    }
        //    else if (imgbtn.ID == "BtnQxt")
        //    {
        //        ChartOutput1.ChartType = SeriesChartType.Bar;
        //    }
        //    //绑定图形
        //    SingleBtn_Click(null, null);
        //}
        private void ShopLnb(int PageCount,int CurPageIdx)
        {
            lnbBeForeChart.Visible = true;
            lnbBehindChart.Visible = true;
           

            int CurNum = CurPageIdx+1;
            if (PageCount == 1)
            {
                lnbBeForeChart.Enabled = false;
                lnbBehindChart.Enabled = false;
            }
            else if (PageCount == 2)
            {
                if (CurNum == 1)
                {
                    lnbBeForeChart.Enabled = false;
                    lnbBehindChart.Enabled = true;
                }
                else if (CurNum == 2)
                {
                    lnbBeForeChart.Enabled = true;
                    lnbBehindChart.Enabled = false;
                }
            }
            else if(PageCount >=3)
            {
                if (CurNum == 1)
                {
                    lnbBeForeChart.Enabled = false;
                    lnbBehindChart.Enabled = true;
                }
                else if (CurNum >= 2 && CurNum < PageCount)
                {
                    lnbBeForeChart.Enabled = true;
                    lnbBehindChart.Enabled = true;
                }
                else if (CurNum == PageCount)
                {
                    lnbBeForeChart.Enabled = true;
                    lnbBehindChart.Enabled = false;
                }
            }
            lbCurPageNO.Text = string.Format("({0}/{1})", CurPageIdx + 1, PageCount);

        }
        protected void drpChartType_Change(object sender,  EventArgs e)
        {
            if (drpChartType.SelectedValue == "FastPoint" || drpChartType.SelectedValue == "FastLine")
            {
                tbxXPointCount.Text = "10000";
                spCountTip.InnerText = "(500~20000)";
            }
            else
            {
                tbxXPointCount.Text = "30";
                spCountTip.InnerText = "(10~100)";
            }
        }
        protected void SingleBtn_Click(object sender, EventArgs e)
        {
                tblBtnImgs.Visible = false;

           
                BLL.BatteryAnaly ba = new BLL.BatteryAnaly();
                //电池主键（联合主键）（用Structure结构体类型传递）
                Model.BatteryKey key = new Model.BatteryKey();
                key.Generation = this.Generation;
                key.EquipID = this.EquipID;
                key.BatteryGrp = this.BatGrp;
                key.BatteryID =  this.BatteryID;

                int pgsize = 0;
                int intv = 3;

                //快速扫描线图
                if (drpChartType.SelectedValue == "FastLine" || drpChartType.SelectedValue == "FastPoint")
                {
                    intv = 3;
                    pgsize = 500;
                }
                else
                {
                    pgsize = int.Parse(tbxXPointCount.Text);
                    TimeSpan ts = (DtmEnd > DtmRealEnd ? DtmRealEnd : DtmEnd) - (DtmBegin < DtmRealBegin ? DtmRealBegin : DtmBegin);
                    double seconds = Math.Ceiling(ts.TotalSeconds);
                    intv = Convert.ToInt32(Math.Ceiling(seconds / pgsize));
                    if (intv < 3) { intv = 3; }//时间间隔不能小于3秒！
                }

               

                //查询和分页的几个参数（用Structure结构体类型传递）
                Model.AnalyParam parms = new Model.AnalyParam();
                parms.dtmBegin = this.DtmBegin;
                parms.dtmEnd = this.DtmEnd;
                parms.interv = intv;
                parms.pageSize = pgsize; //10;
                parms.curPage = int.Parse(hdfCurrentPageIndex.Value);
                

                int PageCount = 0;
                List<Model.BatteryAnaly> list = ba.GetBatteryAbstractData(key, parms, out PageCount);
                ShopLnb(PageCount, parms.curPage);//设置按钮状态

                ///////////////////////////////////////////////////////把分页隐藏掉
                this.lnbBeForeChart.Visible = false;
                this.lnbBehindChart.Visible = false;
                lbCurPageNO.Visible = false;

                System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                this.JsonData1.Text = jsSerializer.Serialize(list);
           
        }

    }
}